<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.transcargo.users.Contact" %>
<%@ page import="com.transcargo.users.User" %>
<%@ page import="com.transcargo.users.Transport" %>
<%@ page import="com.transcargo.errors.ErrorList" %>
<%@ page import="com.transcargo.data.CountriesData" %>
<%@ page import="com.transcargo.geo.Country" %>
<%@ page import="com.transcargo.geo.Region" %>
<%@ page import="com.transcargo.geo.City" %>
<%@ page import="com.transcargo.util.Normalizer" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.StringTokenizer" %>
<%@include file="header.jsf"%>
<%@include file="session.jsf"%>
<%
	String id = request.getParameter("id");
	if (id == null || id.length() == 0 || Integer.parseInt(id) == 0) {
		response.sendRedirect("posts.jsp");
		return;
	}
	HashMap<String,String> loads = null;
	String datetype = "";
	String date_from = "";
	String date_to = "";
	String country_id_from = "";
	String region_id_from = "";
	String city_id_from = "";
	String country_id_to = "";
	String region_id_to = "";
	String city_id_to = "";
	String city_value_from = "";
	String city_value_to = "";
	String owntransport = "";
	String cargo_type = "";
	String volume = "";
	String tonnage = "";
	String length = "";
	String height = "";
	String width = "";
	String tir = "";
	String ekmt = "";
	String adr = "";
	String number = "";
	String payment_type = "";
	String bid = "";
	String payment_curr = "";
	String urgent = "";
	String urgent_time = "";
	String passing = "";
	String comments = "";
	String loading = "";
	String user_id = "";
	String org_name = "";
	String org_address = "";
	String org_city = "";
	String country = "";
	String org_phone = "";
	String org_email = "";
	String org_url = "";
	String org_skype = "";
	String org_icq = "";
	String org_info = "";

	String contact_name = "";
	String contact_phone1 = "";
	String contact_phone2 = "";
	String contact_phone3 = "";
	String contact_email = "";
	String contact_icq = "";
	String contact_skype = "";
	String contact_info = "";
	String contact_photo = "";

	loads = new HashMap<String,String>();
	int Id = Integer.parseInt(id);

	try {
		list = dbase.execute("SELECT date_load, " +
	    		"date_unload, " +
	    		"country_id_from, " +
	    		"region_id_from, " +
	    		"city_id_from, " +
	    		"city_value_from, " +
	    		"country_id_to, " +
	    		"region_id_to, " +
	    		"city_id_to, " +
	    		"city_value_to, " +
	    		"truck_id, " +
	    		"type, " +
	    		"loading, " +
	    		"height, " +
	    		"width, " +
	    		"length, " +
	    		"tir, " +
	    		"ekmt, " +
	    		"adr, " +
	    		"tonnage, " +
	    		"volume, " +
	    		"number, " +
	    		"payment_bid, " +
	    		"payment_type, " +
	    		"payment_currency, " +
	    		"urgent, " +
	    		"urgent_time, " +
	    		"passing, " +
	    		"comments, " +
	    		"permanent, " +
	    		"user_id FROM posted_transport WHERE id=" + Integer.toString(Id));
		if (list != null) {
			iter = list.iterator();
			if (iter.hasNext()) {
				String[] res = iter.next();
				date_from = res[0];
				date_to = res[1];
				country_id_from = res[2];
				region_id_from = res[3];
				city_id_from = res[4];
				city_value_from = res[5];
				country_id_to = res[6];
				region_id_to = res[7];
				city_id_to = res[8];
				city_value_to = res[9];
				owntransport = res[10];
				cargo_type = res[11];
				loading = res[12];
				height = res[13];
				width = res[14];
				length = res[15];
				tir = res[16];
				ekmt = res[17];
				adr = res[18];
				tonnage = res[19];
				volume = res[20];
				number = res[21];
				bid = res[22];
				payment_type = res[23];
				payment_curr = res[24];
				urgent = res[25];
				urgent_time = res[26];
				passing = res[27];
				comments = res[28];
				datetype = res[29];
				user_id = res[30];
				
				StringTokenizer tok = new StringTokenizer(loading,",");
				while (tok.hasMoreTokens()) {
					loads.put(tok.nextToken(),"1");				
				}
			} else throw new Exception();
		} else throw new Exception();
		int ui = Integer.parseInt(user_id);
		int contact_id = ui / 100000000;
		ui = ui % 100000000;
		if (contact_id == 0)
			list = dbase.execute("SELECT registered_users.organization, " + 
								"registered_users.address, " + 
								"registered_users.city, " + 
								"countries.name, " +
								"registered_users.phone, " +
								"registered_users.email, " + 
								"registered_users.web_url, " +
								"registered_users.skype, " +
								"registered_users.icq, " +
								"registered_users.info " +
								"FROM registered_users, countries WHERE countries.id=registered_users.country_id AND registered_users.id=" + ui);
		else
			list = dbase.execute("SELECT registered_users.organization, " + 
					"registered_users.address, " + 
					"registered_users.city, " + 
					"countries.name, " +
					"registered_users.phone, " +
					"registered_users.email, " + 
					"registered_users.web_url, " +
					"registered_users.skype, " +
					"registered_users.icq, " +
					"registered_users.info, " +
					
					"contacts.name, " +
					"contacts.phone1, " +
					"contacts.phone2, " +
					"contacts.phone3, " +
					"contacts.email, " + 
					"contacts.icq, " +
					"contacts.skype, " +
					"contacts.info, " +
					"contacts.photo " +

					"FROM registered_users, countries, contacts WHERE countries.id=registered_users.country_id AND contacts.user_id = registered_users.id AND registered_users.id=" + ui + " AND contacts.id=" + contact_id);
		if (list != null) {
			iter = list.iterator();
			if (iter.hasNext()) {
				String[] res = iter.next();
				org_name = res[0];
				org_address = res[1];
				org_city = res[2];
				country = res[3];
				org_phone = res[4];
				org_email = res[5];
				org_url = res[6];
				org_skype = res[7];
				org_icq = res[8];
				org_info = res[9];
				
				if (contact_id > 0) {
					contact_name = res[10];
					contact_phone1 = res[11];
					contact_phone2 = res[12];
					contact_phone3 = res[13];
					contact_email = res[14];
					contact_icq = res[15];
					contact_skype = res[16];
					contact_info = res[17];
					contact_photo = res[18];
				}
			} else throw new Exception();
		} else throw new Exception();
	} catch (Exception e) {
		response.sendRedirect("posts.jsp");
		return;
	}
	Country ctr_from = new Country(Integer.parseInt(country_id_from), true, dbase);
	Country ctr_to = new Country(Integer.parseInt(country_id_to), true, dbase);
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  	<head><title>TransCargo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 	<link type="text/css" href="transcargo.css" rel="stylesheet"/>
  </head>
  <body>
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr><td>
			<%@include file="page_header.jsf"%>
        </td></tr>
        <tr><td align="center">
            Информация по транспорту<br><br>
         <table border="0">
			<tr><td>Место загрузки:</td><td><%=city_value_from %><br>
			<% if (!region_id_from.equals("0")) out.print(ctr_from.getRegion(Integer.parseInt(region_id_from)).getName() + "<br>"); %>
			<%=ctr_from.getName() %>
			</td></tr>
			<tr><td>Место выгрузки:</td><td><%=city_value_to %><br>
			<% if (!region_id_to.equals("0")) out.print(ctr_to.getRegion(Integer.parseInt(region_id_to)).getName() + "<br>"); %>
			<%=ctr_to.getName() %>
			</td></tr>
			<% if (datetype.equals("1")) {
				%><tr><td colspan="2">Грузы требуются постоянно</td></tr><%
			} else {
			%><tr><td>Дата загрузки:</td><td><%
				out.print(Normalizer.DenormalizeDate(date_from)); 
				if (!date_from.equals(date_to)) out.print(" - " + Normalizer.DenormalizeDate(date_to));
			%></td></tr><%
				if (urgent.equals("1")) {
					%><tr><td colspan="2"><b><font color="red">Срочный груз!<br>Загрузить до <%=urgent_time %></font></b></td></tr><%
				}
			}
			%>
			<tr><td colspan="2"><hr></td></tr>
			<tr><td>Кузов:</td><td><%
				list = dbase.execute("SELECT name FROM transport_type WHERE id=" + cargo_type);
				if (list != null) iter = list.iterator();
				String[] ctr = iter.next();
				out.print(ctr[0]);				
			%></td></tr>
			<tr><td>Тоннаж:</td><td><%= tonnage %> т.</td></tr>
			<tr><td>Объем:</td><td><%= volume %> м<sup>3</sup></td></tr>
<% if (!length.equals("0") && !width.equals("0") && !height.equals("0")) { %>
			<tr><td>Длина:</td><td><%= length %> м.</td></tr>
			<tr><td>Ширина:</td><td><%= width %> м.</td></tr>
			<tr><td>Высота:</td><td><%= height %> м.</td></tr><% } %>
			<tr><td>Загрузка:</td><td><%
				list = dbase.execute("SELECT id, name FROM transport_loading");
				if (list != null) iter = list.iterator();
				while (iter.hasNext()) {
					ctr = iter.next();
					if (loads.containsKey(ctr[0])) out.print(ctr[1]+"<br>");				
				}

			%></td></tr><%
			if (tir.equals("1")) {
				%><tr><td colspan="2">TIR Carnet</td></tr><%
			}
			if (ekmt.equals("1")) {
				%><tr><td colspan="2">EKMT</td></tr><%
			}
			if (!adr.equals("0")) {
				%><tr><td colspan="2">Класс ADR: <%=adr %></td></tr><%
			}
			%><tr><td colspan="2"><hr></td></tr><%
				if (!payment_curr.equals("0")) {
			%><tr><td>Ставка:</td><td><%=bid %> <%
				list = dbase.execute("SELECT name FROM payment_currencies WHERE id=" + payment_curr);
				if (list != null) iter = list.iterator();
				ctr = iter.next();
				out.print(ctr[0]);				
			%></td></tr><%
				} else {
			%><tr><td>Ставка:</td><td>Договорная</td></tr><%
				}
			%><tr><td>Форма оплаты:</td><td><%
			list = dbase.execute("SELECT name FROM payment_types WHERE id=" + payment_type);
			if (list != null) iter = list.iterator();
			ctr = iter.next();
			out.print(ctr[0]);	 
			%></td></tr>
			<% if (comments.length() > 0) {
				%><tr><td colspan="2"><%= comments %></td></tr><%
			} %><tr><td colspan="2"><hr></td></tr>
			<tr><td colspan="2" align="center"><%= org_name %><br><%= org_city %><br><%= country %><br><br><%
			if (contact_name.length() > 0) {
				out.print("<b>" + contact_name+"</b><br>");
				out.print(contact_phone1+"<br>");
				out.print(contact_phone2+"<br>");
				out.print(contact_phone3+"<br>");
				if (contact_icq.length() > 0) out.print("Icq: " + contact_icq + "<br>");
				if (contact_skype.length() > 0) out.print("Skype: " + contact_icq + "<br>");
				if (contact_info.length() > 0) out.print(contact_info + "<br>");
				if (contact_photo.length() > 0) out.print("<img src=\"avatars/" + contact_photo + "\"><br>");
								
			} else {
				out.print(org_phone+"<br>");
				if (!org_url.equals("") && !org_url.equals("http://")) out.print("Web: " + org_url + "<br>");
				if (!org_icq.equals("")) out.print("ICQ: " + org_icq + "<br>");
				if (!org_skype.equals("")) out.print("Skype: " + org_skype + "<br>");
				if (!org_info.equals("")) out.print(org_info + "<br>");
			}
			%>
         </table>
		</td></tr>
    </table>
  </body>
</html>
